Page({

  /**
   * 页面的初始数据
   */
  data: {
    // imgArr: [
    //   'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534241231427&di=3fd6a9caf18f4000babb07eb8e68a286&imgtype=0&src=http%3A%2F%2Fwww.maogp.com%2FUpFile%2F20140421170455.jpg',

    //   "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534241231423&di=fbf6250c8a2aeab06c237d67ed378397&imgtype=0&src=http%3A%2F%2Fpic1.wed114.cn%2Fallimg%2F160421%2F52_160421111914_9.jpg",

    //   "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534241569131&di=7496b50516b12058d85c98c2aaa787b0&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2Fw640h880%2F20171221%2Ff471-fypvuqe7526358.jpg",

    //   "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534241502235&di=9cb6688bb3ee1b055d8ef5ba68579e8e&imgtype=0&src=http%3A%2F%2Fi3.sinaimg.cn%2Fgm%2F2014%2F0902%2FU7232P115DT20140902095707.jpg",

    //   "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534241502235&di=aa6c2499e518494070403c90d0bbb798&imgtype=0&src=http%3A%2F%2Fwx4.sinaimg.cn%2Fnmw690%2Fad5c3905ly1fcy3wlk4tij20j60xzjwe.jpg",

    //   "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534241502229&di=027a0c51e40caaa651c87f70fcb001da&imgtype=0&src=http%3A%2F%2Fimge.gmw.cn%2Fsite2%2F20161227%2Ff44d305ea48819cc1db558.jpeg"

    // ]
  },
  previewImg: function (e) {
    console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var imgArr = this.data.imgArr;
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // loading: function () {
  //   wx.navigateTo({
  //     url: '../pholist/pholist',
  //   })
  // },

  // 删除图片 
  deleteImg: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var pics = this.data.imgArr;
    //console.log(pics)
    for (var i in pics) {
      //console.log(i)
      //console.log(pics[i])
      if (pics[i] == index) {
        pics.splice(i, 1);
      }
    }
    this.setData({
      imgArr: pics
    });
    
    
  },

  //获取用户输入的标题
  titleInput: function (e) {
    this.setData({
      title: e.detail.value.replace(/\s+/g, ''),
    })
  },

  loading: function () {
    var title = this.data.title;
    console.log('title==' + title)

    var imgArr = this.data.imgArr;
    getApp().globalData.imgArr = imgArr


    if (title == undefined || title == "") {
      this.setData({
        error1: '1',
        error: '标题不能为空',
      });
      setTimeout(function () {
        this.setData({
          error1: "2"
        })
      }.bind(this), 1000)
      return;
    }

    if (imgArr == "") {
      this.setData({
        error1: '1',
        error: '图片不能为空',
      });
      setTimeout(function () {
        this.setData({
          error1: "2"
        })
      }.bind(this), 1000)
      return;
    }
    var openid = getApp().globalData.openid;
    var nickname = getApp().globalData.nickName;
    var avatarurl = getApp().globalData.avatarUrl;
    var that = this;
    wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_static_album',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': getApp().globalData.openid,
        'nickname': getApp().globalData.nickName,
        'avatarurl': getApp().globalData.avatarUrl,
        'imgarr': getApp().globalData.imgArr,
        'title': title,
      },
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      method: 'POST',
      success: function (res) {
        console.log("这是上传完图片回调回来的=====================");
        console.log(res.data.static_id);
        wx.navigateTo({
          url: '../pholist/pholist?title=' + title + '&static_id=' + res.data.static_id
        
        })
        
      }
    })



   
  },
  onLoad: function () {
    var selectAlbums = getApp().globalData.selectAlbums;
    console.log("我是上一个页面传过来的");
    console.log(selectAlbums);
    this.setData({ imgArr: selectAlbums, });
    
  },

  
  
})